<?php 

namespace Weixin\Controller;
use Common\Controller\WeixinBase;
use Common\Model\Model;

class LoginController extends WeixinbaseController 
{
    /**
     * 显示登录页面
     */
    public function index()
    {
        if($this->userid)
        {
            header('Location:'.WEB_URL.U('User/index'));
            exit;
        }
        $this->display();
    }
    
    /**
     * 确认账号是否存在
     */
    public function checkname()
    {
        $uname = I('post.uname');
        if(empty($uname))
        {
            $this->error('请输入账号');
        }
        $userid = M('User')->where(array('uname' => $uname))->getField('userid');
        if($userid)
        {
            $isBind = M('WeixinUser')->where(array('userid' => $userid))->count();
            if($isBind)
            {
                $this->error('该账号已经被绑定了');
            }
            $this->success('请输入密码');
        }
        else
        {
            if(!is_email($uname))
            {
                $this->error('输入的账号不存在,请输入邮箱注册新的账号');
            }
            $this->error('输入的账号不存在,请输入密码完成注册绑定');
        }
    }
    
    /**
     * 登录操作
     */
    public function doLogin()
    {
        $loginName = $_POST['uname'];
        if(empty($loginName)){
            $this->error('请输入用户名!');
        }
        $password = $_POST['password'];
        if(empty($password)){
            $this->error('请输入登录密码!');
        }
        $userid = service('Passport')->loginLocal($loginName, $password, 86400);
        
        if ($userid > 0) {
            M('WeixinUser')->where(array('openid' => session('openid')))->save(array('userid' => $userid));
            $userInfo = service("Passport")->getLocalUser((int) $userid);
            $this->success("登陆成功!",U('User/index'));
        } else {
            $this->error('您的帐号或密码错误!');
        }
    }
    
    /**
     * 会员注册
     */
    public function doReg() {
    
        $post = I('post.');
        //用户名
        $post['uname'] = I('post.uname');
    
        if(!is_email($post['uname'])){
            $this->error('用户名必须为邮箱!');
        }
    
        $is_user = M('User')->where(array('uname' => $post['uname']))->count();
        if($is_user > 0)
        {
            $this->error("该邮箱已经被注册了");
        }
    
        $post['nickname']  = M('WeixinUser')->where(array('openid' => session('openid')))->getField('nickname');
        $headimgurl = M('WeixinUser')->where(array('openid' => session('openid')))->getField('headimgurl');
        //设置密码
        $post['upassword'] = I('post.password');
        $pwd = I('post.password2');
        if (empty($post['upassword'])) {
            $this->error('请输入您的密码！');
        }
        if (false == isMin($post['upassword'], 6)) {
            $this->error('密码不能小于6位！');
        }
        if($post['upassword'] != $pwd)
        {
            $this->error("两次输入的密码不一致");
        }
        //验证码
    
        $info = D('User/User')->token(false)->create($post);
        if ($info) {
            $userid = service('Passport')->userRegister($info['uname'], $info['upassword']);
    
            if ($userid > 0) {
                //获取用户信息
                $memberinfo = service("Passport")->getLocalUser((int) $userid);
                $info['uname'] = $memberinfo['uname'];
                $info['upassword'] = $memberinfo['upassword'];
                $info['logtimes']=1;
                $info['login_time']=time();
                $info['lastip']=get_client_ip();
                $info['openuid']=C('BEGIN_UID')+$userid;
                $info['type']='reg';
                $info['islock']=false;
                if(is_email($info['uname'])){
                    $info['uemail']=$info['uname'];
                }elseif(is_mobile($info['uname'])){
                    $info['umobile']=$info['uname'];
                }
                //新注册用户积分
                $info['upoint'] = 0;
    
                if (false !== D('User/User')->where(array('userid' => $memberinfo['userid']))->save($info)) {
                    //获取积分
                    service('Passport')->userDynamic($memberinfo['userid'], 20, '注册获取20个积分', 1, 'register');
                    //注册登陆状态
                    service("Passport")->loginLocal($post['uname'], $post['upassword']);
                    M('WeixinUser')->where(array('openid' => session('openid')))->save(array('userid' => $memberinfo['userid']));
                    M('User')->where(array('userid' => $memberinfo['userid']))->save(array('uavatar' => $headimgurl));
                    $this->success("注册成功,正转向个人中心!", U('User/index'));
                } else {
                    //删除
                    service("Passport")->userDelete($memberinfo['userid']);
                    $this->error("会员注册失败！");
                }
            } else {
                $this->error(service("Passport")->getError()? : '帐号注册失败！');
            }
        } else {
            $this->error(D('User/User')->getError()? : '帐号注册失败！');
        }
    }
    
}